Events in UserControls

In the previous chapter, we defined the properties for our user control, but events can be declared and used! This allows you to cover even more functionality in your control, and then, if necessary, answer your page on some events that are running on the control.

For this chapter, we will prepare a new and very easy user, so that events can be prepared. This will not be a real life purpose, but it is meant to show you how to use events in user control. If you do not know how user control is made and used, please return to some chapters, it will focus on the part of an event.

First of all, we create a new, simple event user controller with this code:


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EventUserControl.ascx.cs" Inherits="EventUserControl" %>
Page title: 
<asp:TextBox runat="server" ID="txtPageTitle" />
<asp:Button runat="server" ID="btnUpdatePageTitle" OnClick="btnUpdatePageTitle_Click" Text="Update" />

All just text and server controls that we know. In the CodeBehind, it looks a bit like this:


public partial class EventUserControl : System.Web.UI.UserControl
{
    private string pageTitle;
    public event EventHandler PageTitleUpdated;

    protected void btnUpdatePageTitle_Click(object sender, EventArgs e)
    {
        this.pageTitle = txtPageTitle.Text;
        if(PageTitleUpdated != null)
            PageTitleUpdated(this, EventArgs.Empty);
    }

    public string PageTitle
    {
        get { return pageTitle; }
    }
}

We have defined a page tetal container variable and an asset for it. Then we have a new thing, an event. As you can see, it is more defined as another type of field, but it is slightly different. The principle of C is explained in the C # tutorial, so we will not be here.

In our button click event, we set the page on the physical field, then we check that PageTitleUpdated, our event is empty. If it is not so, then it means that we have subscribed this incident elsewhere, and in that situation, we send a notification by calling the Penetital Update as a method. As a parameter, we send it as a sender (see user control itself), and an empty event. The RGS parameter will ensure that all the subscribers are informed that the page title has just been updated. Now, in our page, I have announced our User Control in this way:


<%@ Register TagPrefix="My" TagName="EventUserControl" Src="~/EventUserControl.ascx" %>

And inserted it like this:


<My:EventUserControl runat="server" ID="MyEventUserControl" OnPageTitleUpdated="MyEventUserControl_PageTitleUpdated" />

As you can see, we have defined an event handler for PageTitleUpdated events, as it was another server control in our page's CodeBehind, we define simple event handlers for UserControl events like this:


protected void MyEventUserControl_PageTitleUpdated(object sender, EventArgs e)
{
    this.Title = MyEventUserControl.PageTitle;
}

this is easy! Now obviously, updating the page title could have been done directly from our user, so it would be possible to give a simple example to use the events, but as you will learn later, this technique is used in many situations. Can be useful in.